//package cn.chinaunicom.admin.aspect;
//
//import cn.chinaunicom.admin.annotation.AuditLog;
//import cn.chinaunicom.admin.entity.audit_log.AuditLogBean;
//import cn.chinaunicom.admin.entity.enums.LogSubType;
//import cn.chinaunicom.admin.entity.po.SmartCmmntyLog;
//import cn.chinaunicom.admin.service.logs.LogService;
//import cn.chinaunicom.admin.service.mongodb.MongoOperating;
//import cn.chinaunicom.fast.common.utils.UserContextHolder;
//import cn.chinaunicom.fast.common.constant.SmartUser;
//import cn.chinaunicom.fast.common.utils.DateUtil;
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.util.*;
//
///**
// *
// * @ProjectName:    commom.web
// * @Package:        com.springboot.cloud.common.web.aspect
// * @ClassName:      DataScopeAspect
// * @Description:    审计日志，切面处理类
// * @Author:         wangjc
// * @CreateDate:     2020/4/1 0001 14:59
// * @UpdateUser:     更新者
// * @UpdateDate:     2020/4/1 0001 14:59
// * @UpdateRemark:   更新内容
// * @Version:        1.0
// */
//@Slf4j
//@Aspect
//@Component
//public class AuditLogAspect {
//
//	@Resource
//	private LogService logService;
//	@Resource
//	private MongoOperating mongoOperating;
//
//	@Pointcut("@annotation(cn.chinaunicom.admin.annotation.AuditLog)")
//	public void pointCut() {}
//
//	@After("pointCut()")
//	public void saveLog(JoinPoint point) {
//		try {
//			MethodSignature signature = (MethodSignature) point.getSignature();
//			AuditLog auditLog = signature.getMethod().getAnnotation(AuditLog.class);
//			// 获取当前登录人
//			SmartUser smartUser = Optional.ofNullable(UserContextHolder.getInstance().getSmartUser()).orElse(new SmartUser());
//
//			AuditLogBean<SmartCmmntyLog> po = new AuditLogBean<>();
//			po.setCreateTime(new Date());
//			po.setCreateTimeStr(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_SPLIT_PATTERN));
//			po.setCreateUserId(smartUser.getUserId());
//			po.setTenantId(smartUser.getTenantId());
//			po.setTel(smartUser.getTel());
//			po.setDetails(auditLog.details());
//			po.setLogType(auditLog.logType());
//			po.setLogSubType(auditLog.logSubType().getCode());
//			po.setOperationType(auditLog.operationType().getCode());
//			po.setTraceId(auditLog.traceId());
//			po.setLogId(UUID.randomUUID().toString());
//
//			// 社区自定义log
//			SmartCmmntyLog scLog = new SmartCmmntyLog();
//			//获取访问ip
//			RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//			ServletRequestAttributes sar = (ServletRequestAttributes) ra;
//			HttpServletRequest request = sar.getRequest();
//			scLog.setUserBelong(smartUser.getUserBelong());
//			if(StringUtils.isBlank(smartUser.getCommunityId())){
//				scLog.setCommunityId(smartUser.getOrgId());
//			}else {
//				scLog.setCommunityId(smartUser.getCommunityId());
//			}
//			scLog.setCdate(new Date());
//			scLog.setIp(request.getRemoteAddr());
//			scLog.setName(smartUser.getName());
//			scLog.setNumber(smartUser.getLoginName());
//			scLog.setTel(smartUser.getTel());
//			scLog.setModel(auditLog.details());
//			po.setBody(scLog);
//
//			// 操作日志时, 调整详情的文字格式
//			if(LogSubType.OPER_LOG.getCode().equals(auditLog.logSubType().getCode())) {
//				StringBuffer details = new StringBuffer();
//				// 详细操作描述
//				details.append(smartUser.getName());
//				details.append("于 " + po.getCreateTimeStr());
//				details.append(" 对[" + po.getDetails() + "]功能模块");
//				details.append(" 做 [" + po.getOperationType() +"] 操作!");
//				po.setDetails(details.toString());
//			}
//			// 发送kafka
//			logService.save(po);
//			Map map = JSON.parseObject(JSON.toJSONString(po), Map.class);
//			String id = mongoOperating.save(map);
//			log.debug("id = " + id);
//			log.debug("save执行完毕 ");
//			log.debug("日志记录成功: {}" , po.getDetails());
//		} catch (Exception e) {
//			log.error("审计日志切面异常", e);
//		}
//		return;
//	}
//
//}
